package com.cqupt.service.impl;

import com.cqupt.common.Pagination;
import com.cqupt.common.Product;
import com.cqupt.common.ProductShow;
import com.cqupt.dao.TbbussionhallMapper;
import com.cqupt.dao.TbdepositorydataMapper;
import com.cqupt.dao.TbproductMapper;
import com.cqupt.pojo.Tbbussionhall;
import com.cqupt.pojo.Tbdepositorydata;
import com.cqupt.pojo.Tbproduct;
import com.cqupt.pojo.TbproductExample;
import com.cqupt.service.ProductService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ProductServiceImpl implements ProductService {

    @Autowired
    private TbdepositorydataMapper tbdepositorydataMapper;
    @Autowired
    private TbproductMapper tbproductMapper;
    @Autowired
    private TbbussionhallMapper tbbussionhallMapper;



    @Override
    //查询所有产品并显示
    public List productQueryList(Pagination pagination){
        Page<Object> page = PageHelper.startPage(pagination.getPage(), pagination.getLimit(), true);
        //将所有（这里使用了page分页限制这个"所有"）商品用list取出
        List<Tbdepositorydata> tbdepositorydata1 = tbdepositorydataMapper.selectByExample(null);
        List data = new ArrayList();
        //循环获取所有商品以及使用id来得到商品更详细的信息
        for (int i = 0; i < tbdepositorydata1.size(); i++) {
            ProductShow productShow = new ProductShow();
            //获取单条数据记录
            Tbdepositorydata tbdepositorydata = tbdepositorydata1.get(i);
            //获取产品在数据库中的序号（仅仅是在数据库中的序号）
            Integer id = tbdepositorydata.getId();
            //设置编号
            productShow.setProductId(id);
            //获取产品id（外键）
            Integer productid = tbdepositorydata.getProductid();
            //判断是IC卡还是机顶盒，若goodstype==0则表示为IC卡，若goodstype==1则表示为机顶盒
            Integer goodstype = tbproductMapper.selectByPrimaryKey(productid).getGoodstype();
            if(goodstype==0){
                //获取产品名称并设置产品名称
                String productName = tbproductMapper.selectByPrimaryKey(productid).getProductname();
                productShow.setProductName(productName);
                //设置产品类型
                productShow.setGoodsType("数字电视IC卡");
            } else if(goodstype==1) {
                //获取产品名称并设置产品名称
                String productName = tbproductMapper.selectByPrimaryKey(productid).getProductname();
                productShow.setProductName(productName);
                //设置产品类型
                productShow.setGoodsType("数字电视机顶盒");
            }
            //购买人/集团由客户服务提供数据，这里暂且设置为“云域”
            productShow.setProductPurchaser("云域");
            data.add(productShow);
        }
        return data;
    }

    //根据产品ID返回特定商品详细信息
    public Map prouductData(Integer productid){
        Map map = new HashMap();

        //List product = new ArrayList();
        //根据商品id查出此商品在仓库中的记录
        Tbdepositorydata tbdepositorydata = tbdepositorydataMapper.selectByPrimaryKey(productid);
        map.put("depositorydata",tbdepositorydata);
        //这里可以查出此商品所存放的仓库名（可用可不用）
        //String depositoryname = tbdepositorydata.getDepositoryname();
        //获取此商品的实际产品表tbproduct中的序号
        Integer  id = tbdepositorydata.getProductid();
        //根据此id在实际产品表tbproduct中查找它本身的详细信息
        Tbproduct tbproduct = tbproductMapper.selectByPrimaryKey(id);
        map.put("product",tbproduct);
        //String productname = tbproduct.getProductname();
        //Integer modelnum = tbproduct.getModelnum();
        //float price = tbproduct.getPrice();
        //Integer goodstype = tbproduct.getGoodstype();//这个是用来判断商品类型的，0为IC卡1为机顶盒
        //String note = tbproduct.getNote();

        //获取此商品所属的营业厅id
        Integer bussionhallid = tbdepositorydata.getBussionhallid();
        if(bussionhallid == null){
            //如果查询不到对应的营业厅，则显示此错误营业厅数据
            Tbbussionhall tbbussionhall = tbbussionhallMapper.selectByPrimaryKey(2);
            map.put("tbbussionhall",tbbussionhall);
        } else {
            Tbbussionhall tbbussionhall = tbbussionhallMapper.selectByPrimaryKey(bussionhallid);
            map.put("tbbussionhall",tbbussionhall);
        }

        //查询此营业厅名称
        //String bussionhallname = tbbussionhallMapper.selectByPrimaryKey(bussionhallid).getBussionhallname();
        //查询此营业厅辖区
        //String bussionhallarea = tbbussionhallMapper.selectByPrimaryKey(bussionhallid).getBussionhallarea();
        //在下面还可以获取库存，这里暂时先不获取

/*      product.add(depositoryname);
        product.add(productname);
        product.add(modelnum);
        product.add(price);
        product.add(goodstype);
        product.add(note);
        product.add(bussionhallname);
        product.add(bussionhallarea);
       */
        return map;
    }

    //查询特定商品信息
    public Product productListData(Integer  productid){
        Tbdepositorydata tbdepositorydata = tbdepositorydataMapper.selectByPrimaryKey(productid);
        Integer  id = tbdepositorydata.getId();
        Tbproduct tbproduct = tbproductMapper.selectByPrimaryKey(id);
        Integer bussionhallid = tbdepositorydata.getBussionhallid();
        Tbbussionhall tbbussionhall = tbbussionhallMapper.selectByPrimaryKey(bussionhallid);

        Product product = new Product();
        product.setDepositoryName(tbdepositorydata.getDepositoryname());

        if(bussionhallid==null){
            //如果查询不到对应的营业厅，则显示此错误营业厅数据
            Tbbussionhall tbbussionhall1 = tbbussionhallMapper.selectByPrimaryKey(2);
            product.setBussionhallArea(tbbussionhall1.getBussionhallarea());
            product.setBussionhallName(tbbussionhall1.getBussionhallname());
        } else {
            product.setBussionhallArea(tbbussionhall.getBussionhallarea());
            product.setBussionhallName(tbbussionhall.getBussionhallname());
        }
        product.setGoodsType(tbproduct.getGoodstype());
        product.setModelnum(tbproduct.getModelnum());
        product.setNote(tbproduct.getNote());
        product.setPrice(tbproduct.getPrice().toString());
        product.setProductName(tbproduct.getProductname());
        product.setProductPurchaser("云域");
        return product;
    }

    //添加商品
    public int productAdd(Product product){
        int i = 0;
        Integer modelnum = product.getModelnum();
        String productName = product.getProductName();
        Integer goodsType = product.getGoodsType();
        String depositoryName = product.getDepositoryName();
//        String depositoryName = product.getDepositoryName();
//        String note = product.getNote();
//        float price = product.getPrice();
//        String bussionhallArea = product.getBussionhallArea();
//        String bussionhallName = product.getBussionhallName();

        //向tbproduct中添加信息
        Tbproduct tbproduct = new Tbproduct();
        tbproduct.setModelnum(modelnum);
        tbproduct.setProductname(productName);
        tbproduct.setGoodstype(goodsType);
        int i1 = tbproductMapper.insertSelective(tbproduct);

        //向tbdepositorydata中添加信息
        Tbdepositorydata tbdepositorydata = new Tbdepositorydata();

        TbproductExample example = new TbproductExample();
        example.createCriteria().andModelnumEqualTo(modelnum);
        List<Tbproduct> tbproducts = tbproductMapper.selectByExample(example);
        Tbproduct tbproduct1 = tbproducts.get(0);
        Integer productid = tbproduct1.getId();

        tbdepositorydata.setProductid(productid);
        tbdepositorydata.setDepositoryname(depositoryName);
        int i2 = tbdepositorydataMapper.insertSelective(tbdepositorydata);
        i=i1+i2;
        return i;
    }
}
